package civicinity.ist.psu.edu.adapter;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import civicinity.ist.psu.edu.R;
import civicinity.ist.psu.edu.adapter.CivicinityLazyAdapter.ViewHolder;
import civicinity.ist.psu.edu.constants.Constants;
import civicinity.ist.psu.edu.rssfeed.RSSItem;
import civicinity.ist.psu.edu.share.ShareItem;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class CivicinityShareListViewAdapter extends BaseAdapter{

	private Context mContext;
	private LayoutInflater mInflater;
	private ArrayList<ShareItem> mArr;
	private int mLayout;
	private CivicinityImageLoader imageLoader;
	
	public CivicinityShareListViewAdapter(Context context, int layout, ArrayList<ShareItem> arr) {
		mContext = context;
		mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		mLayout = layout;
		mArr = arr;
		
		imageLoader = new CivicinityImageLoader(context.getApplicationContext());
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return mArr.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return mArr.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}
	
	public static class ViewHolder{
        public TextView shareDescription;
        public TextView shareDate;
        public TextView shareCommentCnt;
        public TextView shareLikeCnt;
        public ImageView shareThumbnail;
        public View thumbnail;
    }

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		View vi=convertView;
        ViewHolder holder;
        if(convertView == null){
            vi = mInflater.inflate(R.layout.share_item, null);
            holder = new ViewHolder();
            holder.shareDescription = (TextView)vi.findViewById(R.id.share_description);
            holder.shareDate = (TextView)vi.findViewById(R.id.share_date);
            holder.shareCommentCnt = (TextView)vi.findViewById(R.id.comment_num);
            holder.shareLikeCnt = (TextView)vi.findViewById(R.id.recommend_num);
            holder.shareThumbnail = (ImageView)vi.findViewById(R.id.share_thumbnail);
            vi.setTag(holder);
        }
        else
            holder = (ViewHolder)vi.getTag();
        
        // for title
        holder.shareDescription.setText(mArr.get(position).getEventDescription());
        
        // for comment cnt
        holder.shareCommentCnt.setText("" + mArr.get(position).getEventCommentCnt());
        
        // for like cnt
        holder.shareLikeCnt.setText("" + mArr.get(position).getEventLike());
        
        // for pub date
        SimpleDateFormat sdf = new SimpleDateFormat("MMM dd, yyyy HH:mm");
        Date resultdate = new Date(Long.parseLong(mArr.get(position).getEventDate()) * 1000);
        holder.shareDate.setText(sdf.format(resultdate) + " (" + String.format("%4.1f miles)",  mArr.get(position).getEventDistance() * 0.0006));//  + "," + mArr.get(position).getPubDate());
        
        // for image
        holder.shareThumbnail.setTag(Constants.DOWNLOAD_SHARE_THUMBNAIL + mArr.get(position).getEventPhotoUrl());
        imageLoader.DisplayImage(Constants.DOWNLOAD_SHARE_THUMBNAIL + mArr.get(position).getEventPhotoUrl(), mContext, holder.shareThumbnail);
        
        return vi;
	}

}
